<?php
require_once "kse/core/base.php";
/**
 *
 * @author ixidor
 */

class Core extends kseBasic{
    
    
    public static $path;
    public static $using_modules = FALSE;
    
    //url namespaces for system pages
    public $nsURLs = array();
    //module namespace shows what modules are runnig
    public $nsModules = array();
    //object namespace
    public $nsObject = array();

    public $nsControllers = array();
    
    public static function get(){
        static $db = null;
        if ( $db == null )
          $db = new Core();
        return $db;
    }
    
    public function initialize($path){
        
        $this->parseFile("kse/core/config.xml");
        $this->info("Initialized");
        $this->info("Ends reading config");
        
        $this->path = $path;
        $this->debug("Path is - '". $this->path ."'");
        //Log::outputLog();
        //including dict file
        self::kseRequire($_SERVER['DOCUMENT_ROOT'] ."/kse/core/dicts/". $this->propertyList["SYSTEM_LANG"] .".php", "Reading language dictionary file");
        
        
        //session_start();
        //including modules
        self::kseConnectModules();
              
        //choose module for if sys page or|and display result
        self::ksePreGenerateResult();
        
        //Parsing modules components
        self::kseParseObjects();
        
        $this->nsControllers["coreController"]->printResult();
        
        Log::outputLog();

    }
    
    public function kseDie($message = FALSE){
        if($message){
            $this->error($message);
        }
        $this->error("SYSTEM HALT");
        Log::outputLog();
        die();
    }

        //Core search and connect modules function
    protected function kseConnectModules(){
        
        //including core controller
        self::kseRequire($_SERVER['DOCUMENT_ROOT'] ."/kse/core/coreController.php", "Accesing core controller");
        
        $this->nsControllers["coreController"] = new coreController();
        
        //@todo find where logi breaks
        //thism if for serach sys pages in coreController
        $this->nsModules[] = "core";
        
        if(isset($this->nsModules) && $this->nsModules!=""){
            self::$using_modules = TRUE;
            self::info("Found modules");
            foreach ($this->nsModules as $value) {
                if(self::kseInclude($this->propertyList["PATH_MODULES"] . $value ."/controller.php", "Including module - ". $value)){
                    $controllerName = $value . "Controller";
                    $this->nsControllers[$controllerName] = new $controllerName;
                    $this->nsModules[] = $value;
                }
            }
        }
        else{
            self::info("No modules found");
            self::info("Proccessing in NOMODULE state");
        }
        
    }
    
    //
    protected function ksePreGenerateResult(){
                
        //if sysytem page not found
        $notfound = TRUE;
        foreach($this->nsModules as $moduleController){
            foreach($this->nsURLs[$moduleController."Controller"] as $sysURL){
                if($sysURL == $this->path){
                    $this->info("Found system path");
                    $this->nsControllers[$moduleController."Controller"]->showSysPage();
                    $notfound = FALSE;
                    
                }
            }
        }    
        
        if($notfound){
            
            $this->nsControllers["coreController"]->finder();
        }
        
    }
    
    protected function kseParseObjects(){

        foreach ($this->nsObject as $controller => $object){
            $this->debug("Controller::". $controller ." Object::". $object);
            $this->nsControllers[$controller]->makeResult($object);
        }

    }
    
    public function kseGetModulesFramework(){
        if(self::$using_modules){
            foreach($this->$nsModules as $moduleName){
                if($this->$nsControllers[$moduleName."Controller"]->framework){
                    self::kseRequire($this->propertyList["PATH_MODULES"] . $moduleName ."/framework.php", "Connecting ". $moduleName ."framework");
                }
            }
        }
    }


    public function validateNamespaces($array){
        
        $result = array_map("unserialize", array_unique(array_map("serialize", $array)));

        foreach ($result as $key => $value){
            if(is_array($value)){
                $result[$key] = self::validateNamespaces($value);
            }
        }

        return $result;
        
    }
}
?>